<?php
/**
 *
 * Application for the function: Maintenance
 *
 * This page allows the users to schedule maintenance for their car(s),
 * as well as to view their maintenance history.
 *
 *
 * @author Chi Zhang
 * @version 1.0
 * @package dummy-auto
 *
 */

include('../config.inc');
require('dummy-app.inc');

session_start();
if ( ! (session_id() && isset($_SESSION['unum'])) ) {

        $tree = make_app("Maintenance :: Invalid User!");
        $app = $tree->app;
        $app->addChild('p', "Please Login Before Using This Function.");

        session_end();
        echo $tree->asXML();
        exit;

} elseif ($_GET['action'] == 'schedule') {

        $tree = make_app("Maintenance :: Schedule");
        $app = $tree->app;
        $app->addChild('h2', "Schedule a Maintenance:");

        $app->addChild('form');
        $app->form['action'] = 'app/maintenance.php';
        $app->form->addChild('p', "Please enter the following information:");

        $app->form->addChild('table')->addChild('tr');
        $app->form->table->tr->addChild('td', "The car you want to maintain:");
        $app->form->table->tr->addChild('td')->addChild('input');
        $app->form->table->tr->td[1]->input['type'] = "text";
        $app->form->table->tr->td[1]->input['name'] = "vin";
        $app->form->table->tr->td[1]->input['maxlength'] = "8";

        $app->form->table->addChild('tr');
        $app->form->table->tr[1]->addChild('td', "Date: (yyyy-mm-dd)");
        $app->form->table->tr[1]->addChild('td')->addChild('input');
        $app->form->table->tr[1]->td[1]->input['type'] = "text";
        $app->form->table->tr[1]->td[1]->input['name'] = "date";
        $app->form->table->tr[1]->td[1]->input['maxlength'] = "10";

        $app->form->table->addChild('tr');
        $app->form->table->tr[2]->addChild('td', "Start Time: (hh:mm)");
        $app->form->table->tr[2]->addChild('td')->addChild('input');
        $app->form->table->tr[2]->td[1]->input['type'] = "text";
        $app->form->table->tr[2]->td[1]->input['name'] = "time";
        $app->form->table->tr[2]->td[1]->input['maxlength'] = "5";

        $app->form->addChild('p')->addChild('input');
        $app->form->p[1]->input['type'] = "submit";
        $app->form->p[1]->input['name'] = "sche_submit";
        $app->form->p[1]->input['value'] = "submit";

        $app->form->p[1]->addChild('input');
        $app->form->p[1]->input[1]['type'] = "reset";
        $app->form->p[1]->input[1]['value'] = "reset";


        echo $tree->asXML();
        exit;

} elseif ($_GET['action'] == 'cancel') {

        if (! mysql_query("
                delete from Maintain
                      where WorkNum = $_GET[mid];
                    ") ) exit_404(mysql_error());

        if (! mysql_query("commit;")) exit_404(mysql_error());

} elseif ($_GET['sche_submit']) {

        if (! ($_GET['vin'] && $_GET['date'] && $_GET['time']) )
                exit_404('Empty input field exist, please fill them all!');

        if (! ($car_check = mysql_query("
                        select  VIN
                          from  Sale
                         where  CustomerNum = '$_SESSION[unum]';
                    ")) ) exit_404(mysql_error());

        for ($i = 0, $found = false; $i < mysql_num_rows($car_check); $i++)
        {
                $row = mysql_fetch_array($car_check);
                if ($row[0] == $_GET['vin']) {
                        $found = true;
                        break;
                }
        }
        if (! $found) exit_404("\"$_GET[vin]\" is not your car!");

        if (! mysql_query("
                INSERT INTO Maintain VALUES (
                        0, NULL, '$_GET[vin]', '$_GET[date]', '$_GET[time]',
                        NULL, '', NULL
                );
                    ") ) exit_404(mysql_error());
}


$maintenance = "
        select  M.Date,
                M.TimeS,
                M.TimeE,
                M.VIN,
                E.Name,
                M.WorkPerformed,
                M.Cost
          from  Maintain M,
                Sale S,
                Employee E
         where  S.VIN = M.VIN
           and  M.EmpID = E.EmpID
           and  S.CustomerNum = $_SESSION[unum]
      order by  M.Date Desc,
                M.TimeS Desc;
";


if (! ($sql_list_1 = mysql_query($maintenance)) ) exit_404(mysql_error());
$num_rows_1 = mysql_num_rows($sql_list_1);

$maintain_schedule = "
        select  M.Date,
                M.TimeS,
                M.TimeE,
                M.VIN,
                M.WorkNum
          from  Maintain M,
                Sale S
         where  S.VIN = M.VIN
           and  M.EmpID is NULL
           and  S.CustomerNum = $_SESSION[unum]
      order by  M.Date Desc,
                M.TimeS Desc;
";

if (! ($sql_list_2 = mysql_query($maintain_schedule)) ) exit_404(mysql_error());
$num_rows_2 = mysql_num_rows($sql_list_2);


$tree = make_app("Maintenance");
$app = $tree->app;


$tbl_ndx = 0;
if ($num_rows_2) {
        $app->addChild('h2', "Maintenance Schedule");


        $app->addChild('table')->addChild('tr');
        $app->table->tr->addChild('td', 'Date');
        $app->table->tr->addChild('td', 'Time');
        $app->table->tr->addChild('td', 'Car');

        for ($i=0; $i < $num_rows_2; $i++)
        {
                $app->table->addChild('tr');
                $row = mysql_fetch_array($sql_list_2);

                $tmp = array("$row[0]");
                $tmp[] = "$row[1] -         ";
                $tmp[] = $row[3];

                for ($j=0; $j < 4; $j++)
                        $app->table->tr[$i+1]->addChild('td', $tmp[$j]);


                $app->table->tr[$i+1]->addChild('td')->addChild('input');
                $app->table->tr[$i+1]->td[$j]->input['type'] = 'button';
                $app->table->tr[$i+1]->td[$j]->input['value'] = 'Cancel';
                $app->table->tr[$i+1]->td[$j]->input['onclick'] =
                        "load_app('maintenance', 'action=cancel&mid=$row[4]')";
        }

        $tbl_ndx = 1;
}


$app->addChild('h2', "Maintenance History");

$app->h2->addChild('input');
$app->h2->input['type'] = 'button';
$app->h2->input['value'] = 'Schedule a Maintenance';
$app->h2->input['onclick'] =
        "load_app('maintenance', 'action=schedule')";


if (! $num_rows_1) {
        $app->addChild('p', "No Maintenance History found.");
} else {
        $app->addChild('table')->addChild('tr');
        $app->table[$tbl_ndx]->tr->addChild('td', 'Date');
        $app->table[$tbl_ndx]->tr->addChild('td', 'Time');
        $app->table[$tbl_ndx]->tr->addChild('td', 'Car');
        $app->table[$tbl_ndx]->tr->addChild('td', 'Employee');
        $app->table[$tbl_ndx]->tr->addChild('td', 'Work Performed');
        $app->table[$tbl_ndx]->tr->addChild('td', 'Cost');

        for ($i=0; $i < $num_rows_1; $i++)
        {
                $app->table[$tbl_ndx]->addChild('tr');
                $row = mysql_fetch_array($sql_list_1);

                $tmp = array("$row[0]");
                $tmp[] = "$row[1] - $row[2]";
                $tmp[] = $row[3];
                $tmp[] = $row[4];
                $tmp[] = $row[5];
                $tmp[] = $row[6];

                for ($j=0; $j < 6; $j++)
                        $app->table[$tbl_ndx]->tr[$i+1]->addChild('td', $tmp[$j]);
        }
}

echo $tree->asXML();
?>
